<?php
namespace Home\Controller;

use Think\Controller;
use Think\Upload;

class RunController extends BaseController
{
    //结算未结算（极速赛车）
    public function jieshuai_jssc()
    {
        $periodnumber="31221585";
        $current_number = M('number')->where("game='jspk10' && periodnumber={$periodnumber}")->order('id DESC')->find();
        dump($current_number);
        $number1 = explode(',', $current_number['awardnumbers']);
        $lh = unserialize($current_number['lh']);
        for($i=0;$i<count($number1);$i++){
            if($number1[$i] % 2 == 0){
                $number[$i]['ds'] = '双';
                if($number1[$i]>=6){
                    $number[$i]['zuhe'] = '大双';
                }else{
                    $number[$i]['zuhe'] = '小双';
                }
            }else{
                $number[$i]['ds'] = '单';
                if($number1[$i]>=6){
                    $number[$i]['zuhe'] = '大单';
                }else{
                    $number[$i]['zuhe'] = '小单';
                }
            }
            if($number1[$i]>=6){
                $number[$i]['dx'] = '大';
            }else{
                $number[$i]['dx'] = '小';
            }
        }
        //当前局所有竞猜
        $today_time = strtotime(date('Y-m-d',time()));
        $list = M('order')->where("number = {$current_number['periodnumber']} && state = 1 && is_add = 0 && game='jspk10'")->order("time ASC")->select();
        dump($list);exit;
        if($list){
            for($i=0;$i<count($list);$i++){
                $id = $list[$i]['id'];
                $userid = $list[$i]['userid'];
                if($list[$i]['t_id'] and C('fenxiao_set') == 2){
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('yong', $list[$i]['del_points']*C('fenxiao')*0.01);
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('t_add', $list[$i]['del_points']*C('fenxiao')*0.01);
                    $fx_data = array(
                        'uid' => $userid,
                        't_uid' => $list[$i]['t_id'],
                        'orderid' => $id,
                        'push_money' => $list[$i]['del_points'],
                        'rate' => C('fenxiao')*0.01,
                        'money' => $list[$i]['del_points']*C('fenxiao')*0.01,
                        'time' => time()
                    );
                    M('push_money')->add($fx_data);
                }

                $set_add = M('order')->where("id={$id}")->setField(array('is_add'=>1));

                //分类
                switch($list[$i]['type']){
                    //车号大小单双(12345双100)
                    case 1:
                        $num1 = 0;
                        $ex_info = explode('大', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '大';
                        }
                        $ex_info = explode('小', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '小';
                        }
                        $ex_info = explode('单', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '单';
                        }
                        $ex_info = explode('双', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '双';
                        }

                        // $start1 = explode('/', $list[$i]['jincai']);
                        // $num1 = 0;
                        if ($start1[0] == '') {
                            $starts1 = array('1');
                        } else {
                            $starts1 = str_split($start1[0]);
                        }

                        if($info_dxds=='单' || $info_dxds=='双'){
                            for($a=0;$a<count($starts1);$a++){
                                if($starts1[$a]==0){
                                    $hao1 = '9';
                                }else{
                                    $hao1 = $starts1[$a]-1;
                                }
                                if($number[$hao1]['ds']==$info_dxds){
                                    $num1++;
                                }
                            }
                        }else{
                            for($a=0;$a<count($starts1);$a++){
                                if($starts1[$a]==0){
                                    $hao1 = '9';
                                }else{
                                    $hao1 = $starts1[$a]-1;
                                }
                                if($number[$hao1]['dx']==$info_dxds){
                                    $num1++;
                                }
                            }
                        }
                        if($num1>0){
                            $points1 = $num1*$start1[1]*C('jspk10_dxds');
                            $res1 = $this->add_points($id,$userid,$points1);

                        }
                        break;

                    //车号(12345/89/20)
                    case 2:
                        $start2 = explode('/', $list[$i]['jincai']);

                        if (count($start2) == 3) {
                            $chehao2 = str_split($start2[1]);
                            $starts2 = str_split($start2[0]);
                            $ya = $start2[2];
                        } else {
                            $chehao2 = str_split($start2[0]);
                            $starts2 = str_split('1');
                            $ya = $start2[1];
                        }



                        $num2 = 0;
                        for($s=0;$s<count($chehao2);$s++){
                            for($a=0;$a<count($starts2);$a++){
                                if($starts2[$a]==0){
                                    $hao2 = '9';
                                }else{
                                    $hao2 = $starts2[$a]-1;
                                }
                                if($chehao2[$s]==0){
                                    $chehao2[$s]=10;
                                }
                                if($chehao2[$s]==$number1[$hao2]){
                                    $num2++;
                                }
                            }
                        }
                        if($num2>0){
                            $points2 = $num2*$ya*C('jspk10_chehao');
                            $res2 = $this->add_points($id,$userid,$points2);

                        }
                        break;

                    //组合(890/大单/50)
                    case 3:
                        $start3 = explode('/', $list[$i]['jincai']);
                        $starts3 = str_split($start3[0]);
                        $num3 = 0;
                        for($a=0;$a<count($starts3);$a++){
                            if($starts3[$a]==0){
                                $hao3 = '9';
                            }else{
                                $hao3 = $starts3[$a]-1;
                            }
                            if($number[$hao3]['zuhe']==$start3[1]){
                                $num3++;
                            }
                        }
                        if($num3>0){
                            if($start3[1]=='大单' || $start3[1]=='小双'){
                                $points3 = $num3*$start3[2]*C('jspk10_zuhe_1');
                            }else{
                                $points3 = $num3*$start3[2]*C('jspk10_zuhe_2');
                            }
                            $res3 = $this->add_points($id,$userid,$points3);

                        }
                        break;

                    //龙虎(123/龙/100)
                    case 4:
                        $start4 = explode('/', $list[$i]['jincai']);
                        $starts4 = str_split($start4[0]);
                        $num4 = 0;
                        for($a=0;$a<count($starts4);$a++){
                            if($starts4[$a]==0){
                                $hao4 = '9';
                            }else{
                                $hao4 = $starts4[$a]-1;
                            }
                            if($lh[$hao4]==$start4[1]){
                                $num4++;
                            }
                        }
                        if($num4>0){
                            $points4 = $num4*$start4[2]*C('jspk10_lh');
                            $res4 = $this->add_points($id,$userid,$points4);

                        }
                        break;

                    //冠亚庄闲(庄/200)
                    case 5:
                        $start5 = explode('/', $list[$i]['jincai']);
                        if($current_number['zx'] == $start5[0]){
                            $points5 = $start5[1]*C('jspk10_zx');
                            $res5 = $this->add_points($id,$userid,$points5);

                        }
                        break;

                    //冠亚号码(组/1-9.3-7/100)
                    case 6:
                        $start6 = explode('/', $list[$i]['jincai']);
                        $num6 = 0;
                        if(strlen($start6[1])>3){
                            $zu = explode('.', $start6[1]);
                            for($a=0;$a<count($zu);$a++){
                                $gy = explode('-', $zu[$a]);
                                if($gy[0]==0){
                                    $gy[0]=10;
                                }
                                if($gy[1]==0){
                                    $gy[1]=10;
                                }
                                if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
                                    $num6 = 1;
                                }
                            }
                        }else{
                            $gy = explode('-', $start6[1]);
                            if($gy[0]==0){
                                $gy[0]=10;
                            }
                            if($gy[1]==0){
                                $gy[1]=10;
                            }
                            if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
                                $num6 = 1;
                            }
                        }
                        if($num6>0){
                            $points6 = $num6*$start6[2]*C('jspk10_gy');
                            $res6 = $this->add_points($id,$userid,$points6);

                        }
                        break;

                    //特码大小单双(和双100)
                    case 7:
                        $start7 = substr($list[$i]['jincai'], 3,3);
                        $starts7 = substr($list[$i]['jincai'], 6);
                        $num7 = 0;
                        if($current_number['tema'] == 11 && C('jspk10_gy_set') == 2){
                            $num7 = 2;
                        }else if($start7=='大' || $start7=='小'){
                            if($current_number['tema_dx']==$start7){
                                $num7 = 1;
                            }
                        }else if($start7=='单' || $start7=='双'){
                            if($current_number['tema_ds']==$start7){
                                $num7 = 1;
                            }
                        }
                        if($num7>0){
                            if ($num7 == 1) {
                                if (C('jspk10_gy_set') == 1) {
                                    if($start7=='大' || $start7=='双'){
                                        $points7 = $starts7*C('jspk10_tema_1');
                                    }else{
                                        $points7 = $starts7*C('jspk10_tema_2');
                                    }
                                } else{
                                    $points7 = $starts7*C('jspk10_tema');
                                }

                            } else{
                                $points7 = $starts7*1;
                            }

                            $res7 = $this->add_points($id,$userid,$points7);

                        }
                        break;

                    //特码数字(和5.6.7/100)
                    case 8:
                        $tema1 = array('03','04','18','19');
                        $tema2 = array('5','6','16','17');
                        $tema3 = array('7','8','14','15');
                        $tema4 = array('9','10','12','13');
                        $tema5 = array('11');

                        $start8 = explode('/', $list[$i]['jincai']);
                        $starts8 = substr($start8[0], 3);
                        $num8 = 0;
                        $points8 = 0;

                        /*原代码
                        $num_str8 = str_split($starts8);
                        $info_str8 = array();
                        for($a=0;$a<count($num_str8);$a++){
                            if ($a >= 1) {
                                if ($num_str8[$a-1] == '1') {
                                    if ( $num_str[$a-2] == '1' && $num_str[$a] == '1') {
                                    } else {
                                        continue;
                                    }
                                }
                            }
                            if ($num_str8[$a] == '1') {
                                $info_str8[] = $num_str8[$a] . $num_str8[$a+1];
                            } else {
                                $info_str8[] = $num_str8[$a];
                            }
                        }*/
                        $num_str8 = str_split($starts8);
                        $num_str8_bf=$num_str8;
                        $info_str8 = array();
                        foreach ($num_str8 as $k8=>$v8){
                            if ($num_str8_bf[$k8]){
                                if('1'==$v8){
                                    $info_str8[] = $num_str8_bf[$k8] . $num_str8_bf[$k8+1];
                                    unset($num_str8_bf[$k8+1]);
                                }else{
                                    $info_str8[] = $num_str8_bf[$k8];
                                }
                            }
                        }


                        for($a=0;$a<count($info_str8);$a++){
                            if($current_number['tema']==$info_str8[$a]){
                                if(in_array($info_str8[$a], $tema1)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_1'));
                                }
                                if(in_array($info_str8[$a], $tema2)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_2'));
                                }
                                if(in_array($info_str8[$a], $tema3)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_3'));
                                }
                                if(in_array($info_str8[$a], $tema4)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_4'));
                                }
                                if(in_array($info_str8[$a], $tema5)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_5'));
                                }
                                $num8++;
                            }
                        }

                        if($num8>0){
                            $res8 = $this->add_points($id,$userid,$points8);//竞猜成功  加分

                        }
                        break;

                    //特码区段(BC/100)
                    case 9:
                        $start9 = explode('/', $list[$i]['jincai']);
                        $num9 = 0;
                        if(strlen($start9[0])>1){
                            $starts9 = str_split($start9[0]);
                            for($a=0;$a<count($starts9);$a++){
                                if($current_number['tema_dw']==$starts9[$a]){
                                    if($starts9[$a]=='A' || $starts9[$a]=='C'){
                                        $points9 = $start9[1]*C('jspk10_tema_qd_1');
                                    }else{
                                        $points9 = $start9[1]*C('jspk10_tema_qd_2');
                                    }
                                    $num9 = 1;
                                }
                            }
                        }else{
                            if($current_number['tema_dw']==$start9[0]){
                                if($start9[0]=='A' || $start9[0]=='C'){
                                    $points9 = $start9[1]*C('jspk10_tema_qd_1');
                                }else{
                                    $points9 = $start9[1]*C('jspk10_tema_qd_2');
                                }
                                $num9 = 1;
                            }
                        }
                        if($num9>0 && $points9){
                            $res9 = $this->add_points($id,$userid,$points9);
                        }
                        break;
                }
            }
        }
    }
    //补开奖结果（ssc重启时时彩）--开奖结果没有时使用
    public function buqi_ssc(){
//        //1.添加开奖结果
        $game='ssc';
        $periodnumber="20190924010";
        $s= M('caiji')->where("game = {$game} and periodnumber={$periodnumber}")->find();

        $data = array(
            'periodnumber' => $periodnumber,//期号
            'awardnumbers' => "5,4,8,7,3",//开奖号码

            'awardtime' => "2019-09-24 07:30:45",//开奖时间
            'awardtime_z1' => "2019-09-24 07:50:45",//下期开奖时间
            'game' => $game,//类型
            'addtime' => time(),
        );

        $s= M('caiji')->where("game = 'ssc' and periodnumber={$periodnumber}")->find();
        if (!$s){
            M('caiji')->add($data);
        }
        //2.存每期结果
        $caiji = M('caiji')->where("game={$game}  and periodnumber={$periodnumber}")->order("periodnumber desc")->find();
        $data = json_decode(ssc_format($caiji),true);
        $today_time = date('Y-m-d',time()) . " 00:00:00";
        $where=['periodnumber'=>$data['current']['periodNumber'],
//            'awardtime'=>$data['current']['awardTime'],
            'game'=>$caiji['game']];
        $res = M('number')->where($where)->find();
        if(!$res){
            $map['awardnumbers'] = $data['current']['awardNumbers'];
            $map['awardtime'] = $data['current']['awardTime'];
            $map['awardtime_z1'] = date('Y-m-d H:i:s',$data['next']['n_awardTime']);
            $map['time'] = strtotime($data['current']['awardTime']);
            $map['periodnumber'] = $data['current']['periodNumber'];
            $info = explode(',', $map['awardnumbers']);
            //龙虎合
            $map['number'] = serialize($info);
            if($info[0]>$info[4]){
                $map['lh'] = '龙';
            }else if($info[0] == $info[4]){
                $map['lh'] = '合';
            }else{
                $map['lh'] = '虎';
            }
            $map['tema'] = $info[0]+$info[1]+$info[2]+$info[3]+$info[4];

            //大小单双
            if($map['tema'] % 2 == 0){
                $map['tema_ds'] = '双';
            }
            else{
                $map['tema_ds'] = '单';
            }

            if($map['tema']>=23){
                $map['tema_dx'] = '大';
            }else{
                $map['tema_dx'] = '小';
            }

            //区段
            if($map['tema']>=0 && $map['tema']<=15){
                $map['tema_dw'] = 'A';
            }
            if($map['tema']>=16 && $map['tema']<=29){
                $map['tema_dw'] = 'B';
            }
            if($map['tema']>=30 && $map['tema']<=45){
                $map['tema_dw'] = 'C';
            }

            //前中后
            $map['q3'] = ssc_qzh(array($info[0],$info[1],$info[2]));
            $map['z3'] = ssc_qzh(array($info[1],$info[2],$info[3]));
            $map['h3'] = ssc_qzh(array($info[2],$info[3],$info[4]));

            $map['game'] = $data['game'];
            $res1 = M('number')->add($map);
            dump($res1);
        }
        //3.开奖结果结算
        $current_number = M('number')->where("game = {$game} and periodnumber={$periodnumber}")->order('id DESC')->find();
        $number1 = explode(',', $current_number['awardnumbers']);

        for($i=0;$i<count($number1);$i++){
            if($number1[$i] % 2 == 0){
                $number[$i]['ds'] = '双';
            }else{
                $number[$i]['ds'] = '单';
            }
            if($number1[$i]>=5){
                $number[$i]['dx'] = '大';
            }else{
                $number[$i]['dx'] = '小';
            }
        }
        //当前局所有竞猜
        $today_time = strtotime(date('Y-m-d',time()));
        $list = M('order')->where("number = {$periodnumber} && state = 1 && is_add = 0 && game = {$game}")->order("time ASC")->select();

        if($list){
            for($i=0;$i<count($list);$i++){
                $id = $list[$i]['id'];
                $userid = $list[$i]['userid'];
                if($list[$i]['t_id'] and C('fenxiao_set') == 2){
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('yong', $list[$i]['del_points']*C('fenxiao')*0.01);
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('t_add', $list[$i]['del_points']*C('fenxiao')*0.01);
                    $fx_data = array(
                        'uid' => $userid,
                        't_uid' => $list[$i]['t_id'],
                        'orderid' => $id,
                        'push_money' => $list[$i]['del_points'],
                        'rate' => C('fenxiao')*0.01,
                        'money' => $list[$i]['del_points']*C('fenxiao')*0.01,
                        'time' => time()
                    );
                    M('push_money')->add($fx_data);
                }

                $set_add = M('order')->where("id={$id}")->setField(array('is_add'=>1));

                //分类
                switch($list[$i]['type']){
                    //定位球(12345/89/20)
                    case 1:
                        $start1 = explode('/', $list[$i]['jincai']);

                        if (count($start1) == 3) {
                            $chehao1 = str_split($start1[1]);
                            $starts1 = str_split($start1[0]);
                            $ya2 = $start1[2];
                        } else {
                            $chehao1 = str_split($start1[0]);
                            $starts1 = str_split('1');
                            $ya2 = $start1[1];
                        }

                        $num1 = 0;
                        for($s=0;$s<count($chehao1);$s++){
                            for($a=0;$a<count($starts1);$a++){
                                $hao1 = $starts1[$a]-1;
                                if($chehao1[$s]==$number1[$hao1]){
                                    $num1++;
                                }
                            }
                        }
                        if($num1>0){
                            $points1 = $num1*$ya2*C('ssc_dwq');
                            $res1 = $this->add_points($id,$userid,$points1);

                        }
                        break;

                    //龙虎合(龙100)
                    case 2:
                        $start2 = substr($list[$i]['jincai'], 0,3);
                        $starts2 = substr($list[$i]['jincai'], 3);
                        $num2 = 0;
                        if ($start2 == $current_number['lh']) {
                            $num2 = 1;
                        }

                        if($num2>0){
                            if ($start2 == '龙' || $start2 == '虎') {
                                $points2 = $num2*$starts2*C('ssc_lhh_1');
                            } else if($start2 == '合') {
                                $points2 = $num2*$starts2*C('ssc_lhh_2');
                            }

                            $res2= $this->add_points($id,$userid,$points2);

                        }
                        break;

                    //定位大小单双(12345/双/100)
                    case 3:
                        $start3 = explode('/', $list[$i]['jincai']);
                        $num3 = 0;
                        $starts3= str_split($start3[0]);
                        if($start3[1]=='单' || $start3[1]=='双'){
                            for($a=0;$a<count($starts3);$a++){
                                $hao3 = $starts3[$a]-1;
                                if($number[$hao3]['ds']==$start3[1]){
                                    $num3++;
                                }
                            }
                        }else{
                            for($a=0;$a<count($starts3);$a++){
                                $hao3 = $starts3[$a]-1;
                                if($number[$hao3]['dx']==$start3[1]){
                                    $num3++;
                                }
                            }
                        }
                        if($num3>0){
                            $points3 = $num3*$start3[2]*C('ssc_dxds');
                            $res3 = $this->add_points($id,$userid,$points3);

                        }
                        break;

                    //总数大小单双  大100  小100
                    case 4:
                        $start4 = substr($list[$i]['jincai'], 0,3);
                        $starts4 = substr($list[$i]['jincai'],3);
                        $num4 = 0;

                        if ($start4 == '大' || $start4 == '小') {
                            if ($start4 == $current_number['tema_dx']) {
                                $num4++;
                            }
                        } else {
                            if ($start4 == $current_number['tema_ds']) {
                                $num4++;
                            }
                        }

                        if($num4>0){
                            $points4 = $num4*$starts4*C('ssc_zdxds');
                            $res4 = $this->add_points($id,$userid,$points4);

                        }
                        break;

                    //前中后  前豹子100  中顺子100
                    case 5:
                        $start5 = substr($list[$i]['jincai'], 0,3);
                        $starts5 = substr($list[$i]['jincai'], 3,6);
                        $money5 = substr($list[$i]['jincai'], 9);
                        $num5 = 0;

                        if ($start5 == '前') {
                            if ($starts5 == $current_number['q3']) {
                                $num5 = 1;
                            }
                        } else if($start5 == "中") {
                            if ($starts5 == $current_number['z3']) {
                                $num5 = 1;
                            }
                        } else if($start5 == '后') {
                            if ($starts5 == $current_number['h3']) {
                                $num5 = 1;
                            }
                        }

                        if($num5>0){
                            if ($starts5 == '豹子') {
                                $points5 = $num5*$money5*C('ssc_qzh_1');
                            }
                            if ($starts5 == '顺子') {
                                $points5 = $num5*$money5*C('ssc_qzh_2');
                            }
                            if ($starts5 == '对子') {
                                $points5 = $num5*$money5*C('ssc_qzh_3');
                            }
                            if ($starts5 == '半顺') {
                                $points5 = $num5*$money5*C('ssc_qzh_4');
                            }
                            if ($starts5 == '杂六') {
                                $points5 = $num5*$money5*C('ssc_qzh_5');
                            }

                            $res5 = $this->add_points($id,$userid,$points5);

                        }
                        break;

                    //和值特码区段(BC/100)
                    case 6:
                        $start6 = explode('/', $list[$i]['jincai']);
                        $num6 = 0;
                        if(strlen($start6[0])>1){
                            $starts6 = str_split($start6[0]);
                            for($a=0;$a<count($starts6);$a++){
                                if($current_number['tema_dw']==$starts6[$a]){
                                    if($starts6[$a]=='A' || $starts6[$a]=='C'){
                                        $points6 = $start6[1]*C('ssc_tema_qd_1');
                                    }else{
                                        $points6 = $start6[1]*C('ssc_tema_qd_2');
                                    }
                                    $num9 = 1;
                                }
                            }
                        }else{
                            if($current_number['tema_dw']==$start6[0]){
                                if($start6[0]=='A' || $start6[0]=='C'){
                                    $points6 = $start6[1]*C('ssc_tema_qd_1');
                                }else{
                                    $points6 = $start6[1]*C('ssc_tema_qd_2');
                                }
                                $num6 = 1;
                            }
                        }
                        if($num6>0 && $points6){
                            $res6 = $this->add_points($id,$userid,$points6);

                        }
                        break;

                    //二字玩法(13/13.37/100)
                    case 7:
                        $start7 = explode('/', $list[$i]['jincai']);
                        $num71 = 0;
                        $num72 = 0;

                        $zu = explode('.', $start7[1]);
                        $zu1 = str_split($zu[0]);
                        $zu2 = str_split($zu[1]);
                        $ws = str_split($start7[0]);
                        for($a=0;$a<count($zu1);$a++){
                            if ($number1[$ws[0]-1] == $zu1[$a]) {
                                $num71++;
                            }
                        }
                        for($b=0;$b<count($zu2);$b++){
                            if ($number1[$ws[1]-1] == $zu2[$b]) {
                                $num72++;
                            }
                        }
                        if($num71>0 && $num72>0){
                            $points7 = $num71*$num72*$start7[2]*C('ssc_he');
                            $res7 = $this->add_points($id,$userid,$points7);

                        }
                        break;
                }
                dump($res1);
            }
        }
        echo "000";
    }

    //补开奖结果（xyft幸运飞艇）--开奖结果没有时使用
    public function buqi_xyft(){
//        //1.添加开奖结果
        $game='xyft';
          $periodnumber="191113125";

        $data = array(
            'periodnumber' => $periodnumber,//期号
            'awardnumbers' => "10,01,02,04,09,06,05,03,08,07",//开奖号码

            'awardtime' => "2019-11-13 23:29:35",//开奖时间
            'awardtime_z1' => "2019-11-13 23:34:35",//下期开奖时间
            'game' => $game,//类型
            'addtime' => time(),
        );
        $where=['periodnumber'=>$periodnumber,
//            'awardtime'=>$data['current']['awardTime'],
            'game'=>$game];
        $s= M('caiji')->where($where)->find();
        if (!$s){
            M('caiji')->add($data);
        }
        //2.存每期结果
        $caiji = M('caiji')->where($where)->order("periodnumber desc")->find();
        $data = json_decode(xyft_format($caiji),true);
        $today_time = date('Y-m-d',time()) . " 00:00:00";
       // $where=['periodnumber'=>$data['current']['periodNumber'],
//            'awardtime'=>$data['current']['awardTime'],
       //     'game'=>$caiji['game']];
        $res = M('number')->where($where)->find();
	
        if(!$res){
            $map['awardnumbers'] = $data['current']['awardNumbers'];
            $map['awardtime'] = $data['current']['awardTime'];
            $map['awardtime_z1'] = date('Y-m-d H:i:s',$data['next']['n_awardTime']);
            $map['time'] = strtotime($data['current']['awardTime']);
            $map['addtime'] = date('Y-m-d H:i:s',time());
            $map['periodnumber'] = $data['current']['periodNumber'];
            $info = explode(',', $map['awardnumbers']);
            for($i=0;$i<count($info);$i++){
                if($info[$i]<10){
                    $info[$i] = substr($info[$i], 1);
                }
            }
            $map['number'] = serialize($info);
            if($info[0]>$info[9]){
                $lh[0] = '龙';
            }else{
                $lh[0] = '虎';
            }
            if($info[1]>$info[8]){
                $lh[1] = '龙';
            }else{
                $lh[1] = '虎';
            }
            if($info[2]>$info[7]){
                $lh[2] = '龙';
            }else{
                $lh[2] = '虎';
            }
            if($info[3]>$info[6]){
                $lh[3] = '龙';
            }else{
                $lh[3] = '虎';
            }
            if($info[4]>$info[5]){
                $lh[4] = '龙';
            }else{
                $lh[4] = '虎';
            }
            $map['lh'] = serialize($lh);
            $map['tema'] = $info[0]+$info[1];

            if (C('ft_gy_set') == 1) {
                if($map['tema'] % 2 == 0){
                    $map['tema_ds'] = '双';
                }
                else{
                    $map['tema_ds'] = '单';
                }
            } else {
                if($map['tema'] % 2 == 0){
                    $map['tema_ds'] = '双';
                }else if($map['tema'] == 11){
                    $map['tema_ds'] = '和';
                }
                else{
                    $map['tema_ds'] = '单';
                }
            }


            if (C('ft_gy_set') == 1) {
                if($map['tema']>=12){
                    $map['tema_dx'] = '大';
                }else{
                    $map['tema_dx'] = '小';
                }
            } else {
                if($map['tema']>=12){
                    $map['tema_dx'] = '大';
                }else if($map['tema'] == 11){
                    $map['tema_dx'] = '和';
                }else{
                    $map['tema_dx'] = '小';
                }
            }

            if($map['tema']>=3 && $map['tema']<=7){
                $map['tema_dw'] = 'A';
            }
            if($map['tema']>=8 && $map['tema']<=14){
                $map['tema_dw'] = 'B';
            }
            if($map['tema']>=15 && $map['tema']<=19){
                $map['tema_dw'] = 'C';
            }
            if($info[0]>$info[1]){
                $map['zx'] = '庄';
            }else{
                $map['zx'] = '闲';
            }
            $map['game'] = $data['game'];

            $res1 = M('number')->add($map);
           if ($res1){
               echo '采集结果保存成功<br>';
           }
        }
        //3.开奖结果结算
        $current_number = M('number')->where($where)->order('id DESC')->find();
        $number1 = explode(',', $current_number['awardnumbers']);

        for($i=0;$i<count($number1);$i++){
            if($number1[$i] % 2 == 0){
                $number[$i]['ds'] = '双';
            }else{
                $number[$i]['ds'] = '单';
            }
            if($number1[$i]>=5){
                $number[$i]['dx'] = '大';
            }else{
                $number[$i]['dx'] = '小';
            }
        }
        //当前局所有竞猜
        $today_time = strtotime(date('Y-m-d',time()));
        $list = M('order')->where()->where(["number" => $periodnumber, "state" => 1 , "is_add" => 0 ,"game" =>$game])->order("time ASC")->select();
		
        if($list){
            for($i=0;$i<count($list);$i++){
                $id = $list[$i]['id'];
                $userid = $list[$i]['userid'];
                if($list[$i]['t_id'] and C('fenxiao_set') == 2){
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('yong', $list[$i]['del_points']*C('fenxiao')*0.01);
                    M('user')->where("id = {$list[$i]['t_id']}")->setInc('t_add', $list[$i]['del_points']*C('fenxiao')*0.01);
                    $fx_data = array(
                        'uid' => $userid,
                        't_uid' => $list[$i]['t_id'],
                        'orderid' => $id,
                        'push_money' => $list[$i]['del_points'],
                        'rate' => C('fenxiao')*0.01,
                        'money' => $list[$i]['del_points']*C('fenxiao')*0.01,
                        'time' => time()
                    );
                    M('push_money')->add($fx_data);
                }

                $set_add = M('order')->where("id={$id}")->setField(array('is_add'=>1));

                //分类
                switch($list[$i]['type']){
                    //车号大小单双(12345/双/100)
                    case 1:
                        $ex_info = explode('大', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '大';
                        }
                        $ex_info = explode('小', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '小';
                        }
                        $ex_info = explode('单', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '单';
                        }
                        $ex_info = explode('双', $list[$i]['jincai']);
                        if (count($ex_info) > 1) {
                            $start1 = $ex_info;
                            $info_dxds = '双';
                        }

                        // $start1 = explode('/', $list[$i]['jincai']);
                        $num1 = 0;
                        if ($start1[0] == '') {
                            $starts1 = array('1');
                        } else {
                            $starts1 = str_split($start1[0]);
                        }

                        if($info_dxds=='单' || $info_dxds=='双'){
                            for($a=0;$a<count($starts1);$a++){
                                if($starts1[$a]==0){
                                    $hao1 = '9';
                                }else{
                                    $hao1 = $starts1[$a]-1;
                                }
                                if($number[$hao1]['ds']==$info_dxds){
                                    $num1++;
                                }
                            }
                        }else{
                            for($a=0;$a<count($starts1);$a++){
                                if($starts1[$a]==0){
                                    $hao1 = '9';
                                }else{
                                    $hao1 = $starts1[$a]-1;
                                }
                                if($number[$hao1]['dx']==$info_dxds){
                                    $num1++;
                                }
                            }
                        }
                        if($num1>0){
                            $points1 = $num1*$start1[1]*C('ft_dxds');
                            $res1 = $this->add_points($id,$userid,$points1);

                        }
                        break;

                    //车号(12345/89/20)
                    case 2:
                        $start2 = explode('/', $list[$i]['jincai']);

                        if (count($start2) == 3) {
                            $chehao2 = str_split($start2[1]);
                            $starts2 = str_split($start2[0]);
                            $ya = $start2[2];
                        } else {
                            $chehao2 = str_split($start2[0]);
                            $starts2 = str_split('1');
                            $ya = $start2[1];
                        }


                        $num2 = 0;
                        for($s=0;$s<count($chehao2);$s++){
                            for($a=0;$a<count($starts2);$a++){
                                if($starts2[$a]==0){
                                    $hao2 = '9';
                                }else{
                                    $hao2 = $starts2[$a]-1;
                                }
                                if($chehao2[$s]==0){
                                    $chehao2[$s]=10;
                                }
                                if($chehao2[$s]==$number1[$hao2]){
                                    $num2++;
                                }
                            }
                        }
                        if($num2>0){
                            $points2 = $num2*$ya*C('ft_chehao');
                            $res2 = $this->add_points($id,$userid,$points2);

                        }
                        break;

                    //组合(890/大单/50)
                    case 3:
                        $start3 = explode('/', $list[$i]['jincai']);
                        $starts3 = str_split($start3[0]);
                        $num3 = 0;
                        for($a=0;$a<count($starts3);$a++){
                            if($starts3[$a]==0){
                                $hao3 = '9';
                            }else{
                                $hao3 = $starts3[$a]-1;
                            }
                            if($number[$hao3]['zuhe']==$start3[1]){
                                $num3++;
                            }
                        }
                        if($num3>0){
                            if($start3[1]=='大单' || $start3[1]=='小双'){
                                $points3 = $num3*$start3[2]*C('ft_zuhe_1');
                            }else{
                                $points3 = $num3*$start3[2]*C('ft_zuhe_2');
                            }
                            $res3 = $this->add_points($id,$userid,$points3);

                        }
                        break;

                    //龙虎(123/龙/100)
                    case 4:
                        $start4 = explode('/', $list[$i]['jincai']);
                        $starts4 = str_split($start4[0]);
                        $num4 = 0;
                        for($a=0;$a<count($starts4);$a++){
                            if($starts4[$a]==0){
                                $hao4 = '9';
                            }else{
                                $hao4 = $starts4[$a]-1;
                            }
                            if($lh[$hao4]==$start4[1]){
                                $num4++;
                            }
                        }
                        if($num4>0){
                            $points4 = $num4*$start4[2]*C('ft_lh');
                            $res4 = $this->add_points($id,$userid,$points4);

                        }
                        break;

                    //冠亚庄闲(庄/200)
                    case 5:
                        $start5 = explode('/', $list[$i]['jincai']);
                        if($current_number['zx'] == $start5[0]){
                            $points5 = $start5[1]*C('ft_zx');
                            $res5 = $this->add_points($id,$userid,$points5);

                        }
                        break;

                    //冠亚号码(组/1-9.3-7/100)
                    case 6:
                        $start6 = explode('/', $list[$i]['jincai']);
                        $num6 = 0;
                        if(strlen($start6[1])>3){
                            $zu = explode('.', $start6[1]);
                            for($a=0;$a<count($zu);$a++){
                                $gy = explode('-', $zu[$a]);
                                if($gy[0]==0){
                                    $gy[0]=10;
                                }
                                if($gy[1]==0){
                                    $gy[1]=10;
                                }
                                if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
                                    $num6 = 1;
                                }
                            }
                        }else{
                            $gy = explode('-', $start6[1]);
                            if($gy[0]==0){
                                $gy[0]=10;
                            }
                            if($gy[1]==0){
                                $gy[1]=10;
                            }
                            if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
                                $num6 = 1;
                            }
                        }
                        if($num6>0){
                            $points6 = $num6*$start6[2]*C('ft_gy');
                            $res6 = $this->add_points($id,$userid,$points6);

                        }
                        break;

                    //特码大小单双(和双100)
                    case 7:
                        $start7 = substr($list[$i]['jincai'], 3,3);
                        $starts7 = substr($list[$i]['jincai'], 6);
                        $num7 = 0;
                        if($current_number['tema'] == 11 && C('ft_gy_set') == 2){
                            $num7 = 2;
                        }else if($start7=='大' || $start7=='小'){
                            if($current_number['tema_dx']==$start7){
                                $num7 = 1;
                            }
                        }else if($start7=='单' || $start7=='双'){
                            if($current_number['tema_ds']==$start7){
                                $num7 = 1;
                            }
                        }
                        if($num7>0){
                            if ($num7 == 1) {
                                if (C('ft_gy_set') == 1) {
                                    if($start7=='大' || $start7=='双'){
                                        $points7 = $starts7*C('ft_tema_1');
                                    }else{
                                        $points7 = $starts7*C('ft_tema_2');
                                    }
                                } else{
                                    $points7 = $starts7*C('ft_tema');
                                }
                            } else{
                                $points7 = $starts7*1;
                            }

                            $res7 = $this->add_points($id,$userid,$points7);

                        }
                        break;

                    //特码数字(和567/100)
                    case 8:
                        $tema1 = array('03','04','18','19');
                        $tema2 = array('5','6','16','17');
                        $tema3 = array('7','8','14','15');
                        $tema4 = array('9','10','12','13');
                        $tema5 = array('11');

                        $start8 = explode('/', $list[$i]['jincai']);
                        $starts8 = substr($start8[0], 3);
                        $num8 = 0;
                        $points8 = 0;

                        /*原代码
                        $num_str8 = str_split($starts8);
                        $info_str8 = array();
                        for($a=0;$a<count($num_str8);$a++){
                            if ($a >= 1) {
                                if ($num_str8[$a-1] == '1') {
                                    if ( $num_str[$a-2] == '1' && $num_str[$a] == '1') {
                                    } else {
                                        continue;
                                    }
                                }
                            }
                            if ($num_str8[$a] == '1') {
                                $info_str8[] = $num_str8[$a] . $num_str8[$a+1];
                            } else {
                                $info_str8[] = $num_str8[$a];
                            }
                        }*/
                        $num_str8 = str_split($starts8);
                        $num_str8_bf=$num_str8;
                        $info_str8 = array();
                        foreach ($num_str8 as $k8=>$v8){
                            if ($num_str8_bf[$k8]){
                                if('1'==$v8){
                                    $info_str8[] = $num_str8_bf[$k8] . $num_str8_bf[$k8+1];
                                    unset($num_str8_bf[$k8+1]);
                                }else{
                                    $info_str8[] = $num_str8_bf[$k8];
                                }
                            }
                        }
                        for($a=0;$a<count($info_str8);$a++){
                            if($current_number['tema']==$info_str8[$a]){
                                if(in_array($info_str8[$a], $tema1)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_1'));
                                }
                                if(in_array($info_str8[$a], $tema2)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_2'));
                                }
                                if(in_array($info_str8[$a], $tema3)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_3'));
                                }
                                if(in_array($info_str8[$a], $tema4)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_4'));
                                }
                                if(in_array($info_str8[$a], $tema5)){
                                    $points8 += intval($start8[1]*C('ft_tema_sz_5'));
                                }
                                $num8++;
                            }
                        }

                        if($num8>0){
                            $res8 = $this->add_points($id,$userid,$points8);

                        }
                        break;

                    //特码区段(BC/100)
                    case 9:
                        $start9 = explode('/', $list[$i]['jincai']);
                        $num9 = 0;
                        if(strlen($start9[0])>1){
                            $starts9 = str_split($start9[0]);
                            for($a=0;$a<count($starts9);$a++){
                                if($current_number['tema_dw']==$starts9[$a]){
                                    if($starts9[$a]=='A' || $starts9[$a]=='C'){
                                        $points9 = $start9[1]*C('ft_tema_qd_1');
                                    }else{
                                        $points9 = $start9[1]*C('ft_tema_qd_2');
                                    }
                                    $num9 = 1;
                                }
                            }
                        }else{
                            if($current_number['tema_dw']==$start9[0]){
                                if($start9[0]=='A' || $start9[0]=='C'){
                                    $points9 = $start9[1]*C('ft_tema_qd_1');
                                }else{
                                    $points9 = $start9[1]*C('ft_tema_qd_2');
                                }
                                $num9 = 1;
                            }
                        }
                        if($num9>0 && $points9){
                            $res9 = $this->add_points($id,$userid,$points9);
                        }
                        break;
                }
            }
        }
        echo "000";
    }
    /*
         * 竞猜成功  加分
         * */
    public function add_points($order_id,$userid,$points){
        $res = M('user')->where("id = $userid")->setInc('points',$points);
        if($res){
            $user_points= $this->user_points($userid);
            $res1 = M('order')->where("id = $order_id")->setField(array('is_add'=>'1','add_points'=>$points,'balance'=>$user_points));
        }
        if($res && $res1){
            return 1;
        }
    }
    /*
    * $返回用户余额
    * user_id用户id
    */

    public function user_points($user_id){
        $user_points = M('user')->find($user_id);
        return $user_points['points'];
    }
	/**
	 * Created by PhpStorm.
	 * User: TheWay
	 * Date: 2019/3/23
	 * Time: 23:02
	 * 视讯类 投注记录
	 * game		hundred(百家乐) longhu(龙虎)
	 * page		页数
	 */
	public function hundredRecord(){
		$userinfo = session('user');
		$user_id = $userinfo['id'];
//		$user_id = 16;
		$post = $_REQUEST;
		$arr  = array();
		$data = array();
		$game = $post['game'];
		$page = $post['page'];
		//$limit = $this->pageAction(50,$page);
        $wheretime=strtotime('00:00:00');
        $map=[
            "game"=>$game,
            "userid"=>$user_id,
            "time"=>['egt',$wheretime]//0点后刷新注单
        ];
        $zongji=[];

		$res_data = M("order")->field("id,number,jincai,del_points,time,add_points,is_add")->where($map)->order("id desc")->select();
        $zongji['jin']=0;
        $zongji['chu']=0;

		foreach($res_data as $k=>$v){
			$jincai_data = json_decode($v['jincai'],true);
			$jincai_num = implode('/',$jincai_data);
			$jincai_str = implode('/',array_keys($jincai_data));
			$arr[$k]['time'] = $v['time'];
			$arr[$k]['is_add'] = $v['is_add'];
			$arr[$k]['number'] = $v['number'];
			$arr[$k]['id'] = $v['id'];
			$arr[$k]['jincai'] = $v['jincai'];
			$arr[$k]['type'] = $jincai_str;
            $arr[$k]['points'] = $jincai_num;
            $arr[$k]['jin'] = 0;//进项
            $arr[$k]['chu'] = 0;//出项

            $arr[$k]['points'] = $jincai_num;
			$arr[$k]['odds_points'] = $v['add_points']-$v['del_points'];
            if (1==$v['is_add']){
                if ($arr[$k]['odds_points']>0){//赢前
                    $arr[$k]['jin'] = $arr[$k]['odds_points'];//进项
                }else{
                    $arr[$k]['chu'] = $arr[$k]['odds_points']*-1;//出项
                }
            }else{
                $arr[$k]['jin'] =0;
                $arr[$k]['chu'] =0;
            }
            $zongji['jin']+=$arr[$k]['jin'];
            $zongji['chu']+=$arr[$k]['chu'];
		}
        $zongji['ying']=$zongji['jin']-$zongji['chu'];
		$data['code'] = '200';
		$data['msg']  = '成功';
		$data['content'] = $arr;
        $data['zongji'] = $zongji;
		echo json_encode($data);
	}
	//百家乐开奖
	public function hundred_kj()
	{
		$this->display();
	}

//龙虎斗开奖
	public function longhu_kj()
	{
		$this->display();
	}


//=============================================================

    public function kefu_wx()
    {
        $info = M('config')->where("id =1")->find();
        $this->assign('info', $info);
        $this->display();
    }

    public function index()
    {
		$userid = session('user');
		//查询上级客服信息
		$t_id = $userid['t_id'];
		if($t_id == '0'){
			$kefu = "/Public/Home/img/kefu1.jpg";
		}else{
			$agent_info = M("user")->field("kefu_code")->where("id='$t_id'")->find();
			$kefu = "/Uploads".$agent_info['kefu_code'];
		}
		$_SESSION['kefu'] = $kefu;
        $auth = auth_check(C('auth_code'), $_SERVER['HTTP_HOST']);
        if (! $auth) {
            echo "未授权或授权已过期";
            exit();
        }
        
//        $kefu = M('config')->where("id = 1")->find();
        $is_weixin = is_weixin();
        // 互动列表
        $article = M('hd');
        $count = $article->count();
        $page = new \Think\Page($count, 5);
        $show = $page->show();
        $list = $article->limit($page->firstRow . ',' . $page->listRows)
            ->order("addtime desc")
            ->select();
        
        $this->assign('show', $show);
        $this->assign('list', $list);
        $this->assign("auth", $auth);
        $this->assign('is_weixin', $is_weixin);
        $this->assign('kefu', $kefu);
//        if (C('index_page') == '2') {
//            $this->display("index_2");
//        } else {
//            $this->display("index_1");
//        }
        $this->display("index_1");
    }

    /*20180503 六合彩 add by theway*/
    public function jslhc()
    {
        $auth = auth_check(C('auth_code'), $_SERVER['HTTP_HOST']);
        if (! $auth) {
            echo "未授权或授权已过期";
            exit();
        }
        
        if (C('is_open') == 0) {
            $this->redirect('error');
        }
        
        // 10期结果
        $list = M('number')->where("game='jslhc'")
            ->order("id DESC")
            ->limit(50)
            ->select();
        
        // 创建SDK实例
        $script = &  load_wechat('Script');
        // 获取JsApi使用签名，通常这里只需要传 $ur l参数
        $url = 'http://' . $_SERVER['SERVER_NAME'] . '/Home/Run/index.html';
        $options = $script->getJsSign($url, $timestamp, $noncestr, $appid);
        
//        $kefu = M('config')->where("id = 1")->find();
        
        $is_weixin = is_weixin();
        
        $this->assign('is_weixin', $is_weixin);
        $this->assign('kefu', $_SESSION['kefu']);
        
        $this->assign('list', $list);
        $this->assign('type', $type);
        $this->assign('options', $options);
        $this->display();
    }
//测试专用
    public function dayin()
    {
        dump(range(10,3000,10));exit();

        $hundred = M('hundred')->where("type=0")->order("id desc")->find();
        dump($hundred);
        echo ($hundred->status);

    }

	//百家乐
	public function hundred()
	{
		$auth = auth_check(C('auth_code'), $_SERVER['HTTP_HOST']);
		if (! $auth) {
			echo "未授权或授权已过期";
			exit();
		}

		if (C('is_open') == 0) {
			$this->redirect('error');
		}

		// 20期结果
		$list = M('hundred')->where("type=0")
			->order("id DESC")
			->limit(50)
			->select();
		$is_weixin = is_weixin();
		$this->assign('is_weixin', $is_weixin);

		$this->assign('kefu', $_SESSION['kefu']);
		$this->assign('list', $list);
		$this->display();
	}
	//龙虎斗
	public function longhu()
	{

        $auth = auth_check(C('auth_code'), $_SERVER['HTTP_HOST']);
		if (! $auth) {
			echo "未授权或授权已过期";
			exit();
		}
		if (C('is_open') == 0) {
			$this->redirect('error');
		}
		// 20期结果
		$list = M('hundred')->where("type=1")
			->order("id DESC")
			->limit(50)
			->select();

//		$kefu = M('config')->where("id = 1")->find();

		$is_weixin = is_weixin();

		$this->assign('is_weixin', $is_weixin);
//		$this->assign('kefu', $kefu);
		$this->assign('kefu', $_SESSION['kefu']);
		$this->assign('list', $list);
		$this->display();
	}


	
	public function openurl()
    {
		$url=$_GET['url'];
		
		$is_weixin = is_weixin();
		if($is_weixin){
			$this->display();
		}else{
			header('Location:'.$url);
		}
		
		
	}


//=======================竞猜===================================================
	//竞猜百家乐
	public function jincaihundred()
	{
		$uid = $_GET['uid'];
		if(!isset($uid)){
			$uid = 0;
		}
		$user_info = M("user")->field("t_id")->where("id='$uid' and status=1")->find();
		$t_id = $user_info['t_id'];
		// 聊天信息
        $where=[
//            'uid'=>['in',[$t_id,0]]
        ];
		$list = M('message')
            ->where("status=1 and game='hundred'")//and uid='$t_id'
            ->where($where)
			->order("id DESC")
			->limit(15)
			->select();

		$this->assign('list', $list);
		$this->display();
	}
	//龙虎斗
	public function jincailonghu()
	{
		$uid = $_GET['uid'];
		if(!isset($uid)){
			$uid = 0;
		}
		$user_info = M("user")->field("t_id")->where("id='$uid'&& status=1")->find();
		$t_id = $user_info['t_id'];
        $where=[
//            'uid'=>['in',[$t_id,0]]
        ];
		// 聊天信息
		$list = M('message')->where("status=1 and game='longhu'")
            ->where($where)
			->order("id DESC")
			->limit(15)
			->select();
		$this->assign('list', $list);
		$this->display();
	}


	/* 竞猜 */
    public function jincaier75sc()
    {
        // 聊天信息
        $list = M('message')->where("status=1 and game='er75sc'")
            ->order("id DESC")
            ->limit(50)
            ->select();
        $this->assign('list', $list);
        $this->display();
    }

//==================开奖记录===========================================
	public function kj222(){
		$list = M('hundred')->where("type='0' and is_ok='0' and add_time>=$statr_time")->order("id asc")->limit(60)->select();
		dump ($list);
	}
	//百家乐
	public function kjhundred()
	{
		$data = array();
		$arr  = array();
		// 返回当日历史数据
		$statr_time = strtotime(date("Ymd",time()));
		$stop_time  = time();
		$list = M('hundred')->where("type='0' and is_ok='0' and status='2'")->order("id asc")->limit(60)->select();
		foreach($list as $k=>$v){
			$arr[$k]['id'] = $v['id'];
			$arr[$k]['period_number'] = $v['period_number'];
			$arr[$k]['award_numbers'] = $v['award_numbers'];
		}
		$data['code'] = '200';
		$data['msg'] = 'success';
		$data['content'] = $arr;
		echo json_encode($data);
	}
    //龙虎斗
	public function kjlonghu()
	{
		$data = array();
		$arr  = array();
		// 返回当日历史数据
		$statr_time = strtotime(date("Ymd",time()));
		$stop_time  = time();

		$list = M('hundred')->where("type='1' and is_ok='0' and status='2'")->select();
		foreach($list as $k=>$v){
			$arr[$k]['id'] = $v['id'];
			$arr[$k]['period_number'] = $v['period_number'];
			$arr[$k]['award_numbers'] = $v['award_numbers'];
		}
		$data['code'] = '200';
		$data['msg'] = 'success';
		$data['content'] = $arr;
		echo json_encode($data);
	}




    
    // 推广二维码
    public function tui()
    {
        $uid = I('uid');
        $userinfo = M('user')->where("id={$uid}")->find();

        if($userinfo['is_agent'] == '0'){
//        	$this->redirect("Home/User/index","","","您还不是代理");
			$this->error("您还不是代理");
		}
        $this->assign('tui', $userinfo['qrcode']);
        $siteurl = $_SERVER['SERVER_NAME'];
        $url = 'http://' . $siteurl . '?t=' . $uid;
        $this->assign('url', $url);
        $this->display();
    }

    /* 注单记录 */
    public function record($t,$game)
    {
    	if(!isset($t)){
			$t = I('t');
		}

		if(!isset($game)){
			$game = I('game');
		}

        $map = array();
        if ($t == 1) {//
            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        }
        if ($t == 2) {
            $beginToday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        }
        if ($t == 3) {
            $beginToday = mktime(0, 0, 0, date('m'), date('d') - 2, date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')) - 1;
        }
        if ($t == 4) {//ssc
            $beginToday = 0;
            $endToday = 0;
        }
        if ($beginToday and $endToday) {
            $map['time'] = array(
                array(
                    'egt',
                    $beginToday
                ),
                array(
                    'elt',
                    $endToday
                ),
                'and'
            );
        }
        $userinfo = session('user');
        $map['state'] = 1;
        $map['userid'] = $userinfo['id'];
        if(isset($game)){
			$map['game'] = $game;
		}

        $order = M('order');
        $count = $order->where($map)->count();
        $points_tj = $order->field("count(id) as count,sum(add_points) as sum_add,sum(del_points) as sum_del")
            ->where($map)
            ->find();
        $points_tj['ying'] = $points_tj['sum_add'] - $points_tj['sum_del'];
        $page = new \Think\Page($points_tj['count'], 50);
        $show = $page->show();
        $wheretime=strtotime('00:00:00');//早上5：00
        $list['list'] = $order->field("*")
            ->where($map)
            ->where("time>={$wheretime}")//（彩种每天早上5：00清零 ）
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order("id DESC")
            ->select();
        //流水与输赢
        $order2 = $order->where($map)->where("time>={$wheretime}")->select();
        $liuying=['liushui'=>0,'ying'=>0];
        foreach ($order2 as $k=>$v){
            $liuying['liushui']+=$v['del_points'];
            if (1==$v['is_add']){
                $liuying['ying']+=$v['add_points']-$v['del_points'];
            }
        }
        $list['liuying']=$liuying;
        $number = array();
        for ($i = 0; $i < count($list); $i ++) {
            if (! in_array($list[$i]['number'], $number)) {
                $number[] = $list[$i]['number'];
            }
            for ($a = 0; $a < count($number); $a ++) {
                if ($list[$i]['number'] == $number[$a]) {
                    $list1[$a]['number'] = $number[$a];
                    $list1[$a]['game'] = $list[$i]['game'];
                    $list1[$a]['order'][] = $list[$i];
                }
            }
        }
        echo json_encode($list);
//        $this->display();
    }


    /* 查询分数 */
    public function check_points()
    {
        if (IS_POST) {
            if (IS_AJAX) {
                $id = I('id');
                if ($id) {
                    $userinfo = M('user')->where("id = $id")->find();
                    if ($userinfo) {
                        $userinfo['error'] = 0;
                    }
                }
                $this->ajaxReturn($userinfo);
            }
        }
    }

    public function del()
    {
		
		
        $id = I('id');
        $info = M('order')->where("id = $id")->find();
        if (! $info) {
            $data['error'] = 0;
            $data['msg'] = '未找到订单';
        } else {
			$game = $info['game'];
            $number = $info['number'];
			$awardnumbers = M('number')->where("periodnumber = '$number' and game = '$game'")->order('id desc')->find();
			if($game == 'er75sc'){
				$game_state = C('er75sc_state');
			}else if($game == 'pk10'){
				$game_state = C('pk10_state');
			}else if($game == 'ssc'){
				$game_state = C('ssc_state');
			}else if($game == 'jnd28'){
				$game_state = C('jnd28_state');
			}else if($game == 'bj28'){
				$game_state = C('bj28_state');
			}else if($game == 'xyft'){
				$game_state = C('xyft_state');
			}else if($game == 'lhc'){
			    $game_state = C('lhc_state');
			}else if($game == 'jslhc'){
                $game_state = C('jslhc_state');
            }
			if ($awardnumbers['awardnumbers'] == '' && $game_state == '1') {
				$res = M('order')->where("id = $id")->setField('state', 0);
				if ($res) {
					$data['error'] = 0;
					// 加分
					M('user')->where("id = {$info['userid']}")->setInc('points', $info['del_points']);
				} else {
					$data['error'] = 1;
					$data['msg'] = '删除失败';
				}
			} else {
				$data['error'] = 1;
				$data['msg'] = '本期已封盘';
			}
        }
        $this->ajaxReturn($data);
        
        // $state = F('state');
        // $pkdata = F('pk10data');
        // if($state==1){
        // $id = I('id');
        // $info = M('order')->where("id = $id")->find();
        // if($info['number']==$pkdata['next']['periodNumber']){
        // $res = M('order')->where("id = $id")->setField('state',0);
        // if($res){
        // $data['error']==1;
        // //加分
        // M('user')->where("id = {$info['userid']}")->setInc('points',$info['del_points']);
        // }else{
        // $data['error']==0;
        // $data['msg']=='删除失败';
        // }
        // }else{
        // $data['error']==0;
        // $data['msg']=='本期已封盘';
        // }
        // }else{
        // $data['error']==0;
        // $data['msg']=='本期已封盘';
        // }
    }
	
	

    public function hd()
    {
        $id = I('id');
        $info = M('hd')->where("id = {$id}")->find();
        $this->assign("info", $info);
        $this->display();
    }

    public function kaijiang()
    {
        $type = I('game');
        // 20期结果
        $list = M('number')->where("game='{$type}'")
            ->order("periodnumber DESC")
            ->limit(50)
            ->select();
        foreach($list as $k=>$v){
        	$list[$k]['lh'] = unserialize($v['lh']);
			$list[$k]['number'] = unserialize($v['number']);
		}
        echo json_encode($list);
//        $this->assign("list", $list);
//        $this->display();
    }
}


?>